package com.eshore.mq.producer.controller;

import com.eshore.mq.Mqtt.MqttProducer;
import com.eshore.mq.producer.service.MqProducerService;
import org.apache.activemq.command.ActiveMQQueue;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.Destination;

/**
 * @author ZhouXiong
 * @Description
 * @apiDefine Created by ZhouX on 2021/9/6
 */
@RestController
public class SendMessageController {

    @Autowired
    MqProducerService mqProducerService;

    @Autowired
    MqttProducer mqttProducer;

    @RequestMapping(value = "/sendMsg", method = RequestMethod.GET)
    public String sendMsg() {
        Destination destination = new ActiveMQQueue("test.topic");
        mqProducerService.sendMessage("Mq你好", destination);
        return "Mq消息发送成功";
    }

    @RequestMapping(value = "/sendMqttMsg", method = RequestMethod.GET)
    public String sendMqttMsg() throws MqttException {
        mqttProducer.pub("mqtt", "hello");
        return "Mq消息发送成功";
    }


    @RequestMapping(value = "/getMqttMsg", method = RequestMethod.GET)
    public String getMqttMsg() throws MqttException {
        mqttProducer.sub("mqtt");
        return "ok";

    }
}
